<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            list-style: none;
        }

        .banner {
            width: 500px;
            height: 300px;
            margin: 50px auto;
            position: relative;
            overflow: hidden;
        }

        .list {
            width: 2500px;
            display: flex;

            position: absolute;
        }

        .banner ul li {
            width: 500px;
            height: 300px;

            background-color: #f00;
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 50px;



            pointer-events: none;
        }

        .banner span {
            position: absolute;
            top: 0;
            bottom: 0;
            margin: auto;
            width: 30px;
            height: 30px;
            background-color: rgba(0, 0, 0, .5);
            color: #fff;
            text-align: center;
            line-height: 30px;
        }

        .next {
            right: 0;
        }

        ol {
            position: absolute;
            width: 100%;
            display: flex;
            justify-content: center;
            bottom: 0;
        }

        ol li {
            width: 20px;
            height: 20px;
            margin: 10px;
            background-color: #666;
        }

        .active {
            background-color: #ff0;
        }
    </style>
</head>

<body>


    <div class="banner">
        <ul class="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>1</li>
        </ul>
        <ol>
            <li class="item active"></li>
            <li class="item"></li>
            <li class="item"></li>
            <li class="item"></li>
        </ol>
        <span class="prev"></span>
        <span class="next"></span>
    </div>

    <script src="../js/jquery.min.js"></script>
    <script>


        function Banner(ele) {
            this.oBox = $(ele);
            this.oUl = this.oBox.find('ul');
            this.oImgs = this.oUl.find('li');
            this.oDots = this.oBox.find('ol li');
            this.oPrev = this.oBox.find('.prev');
            this.oNext = this.oBox.find('.next');

            // 
            this.index = 0;
            this.timer = null;


            // this.oImgs[0]  的时候拿到的是标签  --- 实例化！！！！！！
            this.w = $(this.oImgs[0]).width();

            this.flag = true;

            this.autoPlay();

            this.event();
        }


        Banner.prototype.autoPlay = function () {
            // 定时器
            this.timer = setInterval(() => {
                this.index++;
                this.flag = false;
                this.oUl.animate({
                    left: -this.index * this.w
                }, () => {
                    // 动画播放完毕
                    this.flag = true;

                    if (this.index === this.oImgs.length - 1) {
                        this.index = 0;
                        this.oUl.css('left', 0)
                    }
                })
                this.dot()
            }, 1000)
        }


        Banner.prototype.dot = function () {
            // 小点也变
            if (this.index === this.oImgs.length - 1) {
                this.oDots.removeClass('active').eq(0).addClass('active')
            } else {
                this.oDots.removeClass('active').eq(this.index).addClass('active')
            }
        }

        Banner.prototype.event = function () {
            // 上翻页
            this.oPrev.on('click', () => {
                if (this.flag) {
                    this.flag = false;
                    clearInterval(this.timer);
                    if (this.index === 0) {
                        this.index = this.oImgs.length - 1;
                        this.oUl.css('left', -this.index * this.w)
                    }
                    this.index--;
                    this.oUl.animate({
                        left: -this.index * this.w
                    }, () => {
                        this.flag = true;
                    })
                    this.dot()
                }

            })
            // 下翻页
            this.oNext.on('click', () => {
                clearInterval(this.timer);
                if (this.index === this.oImgs.length - 1) {
                    this.index = 0;
                    this.oUl.css('left', -this.index * this.w)
                }
                this.index++;
                this.oUl.animate({
                    left: -this.index * this.w
                })
                this.dot()
            })

            // 小点切换
            const _this = this;  // !!!!!!!!!!!!!!!!!!!!!!!
            this.oDots.on('click', function () {
                clearInterval(_this.timer);
                _this.index = $(this).index();
                _this.oUl.animate({
                    left: -_this.index * _this.w
                })
                _this.dot()
            })
        }



        new Banner('.banner')


    </script>

</body>

</html>